package com.springsecurity.demo.dao;

import com.springsecurity.demo.auth.ApplicationUser;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import static com.springsecurity.demo.vo.ApplicationUserRole.*;

/**
 * DESC:
 *
 * @author : yj
 * @since : 2020-08-22
 */

@Repository("fake")
public class FakeApplicationUserDaoService implements ApplicationUserDao {

    private final PasswordEncoder passwordEncoder;

    public FakeApplicationUserDaoService(PasswordEncoder passwordEncoder) {
        this.passwordEncoder = passwordEncoder;
    }

    /**
     * 根据 username 查询 applicationUser
     *
     * @param username
     * @return
     */
    @Override
    public Optional<ApplicationUser> selectApplicationUserByUsername(String username) {
        return getApplicationUser()
                .stream()
                .filter(applicationUser -> username.equals(applicationUser.getUsername()))
                .findFirst();
    }

    private List<ApplicationUser> getApplicationUser() {
        return Arrays.asList(
                new ApplicationUser(ADMIN.getGrantedAuthorities(),
                        "admin",
                        passwordEncoder.encode("123"),
                        true,
                        true,
                        true,
                        true),
                new ApplicationUser(ADMINTRAINEE.getGrantedAuthorities(),
                        "tom",
                        passwordEncoder.encode("123"),
                        true,
                        true,
                        true,
                        true),
                new ApplicationUser(STUDENT.getGrantedAuthorities(),
                        "linda",
                        passwordEncoder.encode("123"),
                        true,
                        true,
                        true,
                        true)
        );
    }
}
